<?php
class Admin extends Controller
{
	
	function __construct()
	{
		parent::Controller();
		$this->load->model('admin_model','model');
		$this->load->helper('file');
		$this->load->helper('form');
		$this->load->library('form_validation');
?>
	<!DOCTYPE html>
	<html>
	<head>
		<title>Install syntaxCMS</title>
		<style type="text/css">
			dl, fieldset { clear: both; width: 450px;		   }
			fieldset	 { margin-bottom: 5px;				  }
			legend	   { font-weight: bold;				   }
			.title	   { font-size: 1.5em; }
			.log		 { width: 600px; height: 400px; 
						   overflow: auto; font-size: 0.8em;
						   border: 1px solid #C5C5C5;
																}
			dt		   { margin-left: 0; float:left;		  }
			dd		   { margin-left: 0; float:right;		 }
			*			{ font-family: Verdana;
						   font-size: 1em; line-height: 1.2em;
						   letter-spacing: 0; text-align: left;
						   color: #444;						 }
		</style>
	</head>
	<body>
		<h1 class="title">Install SyntaxCMS</h1>
<?php
	}
	
	function Admin()
	{
		self::__construct();
	}
	
	function index()
	{
		//If the config file does not exist, complain because if we 
		//make one it'll have the wrong perms.
		if (!is_file('config.php') ) {
			die('No config.php file in the base directory.');
		}
		
		// Read the config.php file.
		$config = read_file('config.php');
		
		//Set validation rules.
		$this->form_validation->set_rules('hostname', 'Hostname', 'trim|required');
		$this->form_validation->set_rules('dbusername', 'Databse Username', 'trim|required');
		$this->form_validation->set_rules('dbpassword', 'Password Confirmation', 'required');
		$this->form_validation->set_rules('database', 'Database Name', 'trim|required');
		$this->form_validation->set_rules('dbprefix', 'Database Prefix', 'trim|required');
		
		$this->form_validation->set_rules('username', 'Username', 'trim|required');
		$this->form_validation->set_rules('passwordagain', 'Password Confirmation', 'required|matches[password]');
		$this->form_validation->set_rules('email', 'Email', 'trim|required|valid_email');
		$this->form_validation->set_rules('display_name', 'Display Name', 'trim|required');
		
		$this->form_validation->set_rules('sitename', 'Site Name', 'trim|required');
		$this->form_validation->set_rules('siteurl', 'Site Url', 'trim|required');
		$this->form_validation->set_rules('system_email', 'Administrator Email', 'trim|required|valid_email');
		$this->form_validation->set_rules('contact_email', 'Contact Email', 'trim|required|valid_email');
		
		
		if ($this->form_validation->run() == FALSE)
		{
?>
		<?php
		if ( validation_errors() ){
?>
			<div class="error"><?php echo validation_errors() ?></div>
<?php
		}
		?>
			<p>Fill in the following form and press accept to install syntaxCMS</p>
			<?php echo form_open(); ?>
				<fieldset>
					<legend>Database Info</legend>
					<dl>
						<dt><label for="hostname">Hostname</label></dt>
						<dd><input type="text" name="hostname" value="<?php echo set_value('hostname'); ?>" /></dd>
					</dl>
					
					<dl>
						<dt><label for="dbusername">Username</label></dt>
						<dd><input type="text" name="dbusername" value="<?php echo set_value('dbusername'); ?>" /></dd>
					</dl>
					
					<dl>
						<dt><label for="dbpassword">Password</label></dt>
						<dd><input type="password" name="dbpassword" /></dd>
					</dl>
					
					<dl>
						<dt><label for="database">Database Name</label></dt>
						<dd><input type="text" name="database" value="<?php echo set_value('database'); ?>"/></dd>
					</dl>
					
					<dl>
						<dt><label for="dbprefix">Database Prefix</label></dt>
						<dd><input type="text" name="dbprefix" value="<?php echo set_value('dbprefix'); ?>" /></dd>
					</dl>
				</fieldset>
				
				<fieldset>
					<legend>Administrator Info</legend>
					<dl>
						<dt><label for="username">Username</label></dt>
						<dd><input type="text" name="username" value="<?php echo set_value('username'); ?>"/></dd>
					</dl>
					
					<dl>
						<dt><label for="password">Password</label></dt>
						<dd><input type="password" name="password" /></dd>
					</dl>
					<dl>
						<dt><label for="passwordagain">Password Again</label></dt>
						<dd><input type="password" name="passwordagain" /></dd>
					</dl>
					<dl>
						<dt><label for="email">Email</label></dt>
						<dd><input type="text" name="email" value="<?php echo set_value('email'); ?>" /></dd>
					</dl>
					
					<dl>
						<dt><label for="display_name">Display Name</label></dt>
						<dd><input type="text" name="display_name" value="<?php echo set_value('display_name'); ?>" ></dd>
					</dl>
				</fieldset>
				
				<fieldset>
					<legend>Site Options</legend>
					<dl>
						<dt><label for="sitename">Site Name</label></dt>
						<dd><input type="text" name="sitename" value="<?php echo set_value('sitename');?>"/></dd>
					</dl>
					<dl>
						<dt><label for="siteurl">Site URL</label></dt>
						<dd><input type="text" name="siteurl" value="http://<?php echo $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];?>"/></dd>
					</dl>
					<dl>
						<dt><label for="system_email">System Email</label></dt>
						<dd><input type="text" name="system_email" value="<?php echo set_value('system_email');?>"/></dd>
					</dl>
					<dl>
						<dt><label for="contact_email">Contact Email</label></dt>
						<dd><input type="text" name="contact_email" value="<?php echo set_value('contact_email');?>"/></dd>
					</dl>
				</fieldset>
				<input type="submit" value="Submit" />
			</form>
		</body>
<?php
		} else {
?>
			<p>SyntaxCMS is now being Configured...</p>
<?php
			//Create a random 32byte string for use as an encryption key
			$encryption = $this->model->random_hash(32);
			//Replace the empty values with the config options.
			$config = str_replace('$db[\'default\'][\'hostname\'] = "";','$db[\'default\'][\'hostname\'] = "'.$this->input->post('hostname').'";',$config);
			$config = str_replace('$db[\'default\'][\'username\'] = "";','$db[\'default\'][\'username\'] = "'.$this->input->post('dbusername').'";',$config);
			$config = str_replace('$db[\'default\'][\'password\'] = "";','$db[\'default\'][\'password\'] = "'.$this->input->post('dbpassword').'";',$config);
			$config = str_replace('$db[\'default\'][\'database\'] = "";','$db[\'default\'][\'database\'] = "'.$this->input->post('database').'";',$config);
			$config = str_replace('$db[\'default\'][\'dbprefix\'] = "";','$db[\'default\'][\'dbprefix\'] = "'.$this->input->post('dbprefix').'";',$config);
			$config = str_replace('$config[\'encryption_key\'] = "";','$config[\'encryption_key\'] = "'.$encryption.'";',$config);
			
			//chmod the file first.
			@chmod('config.php', 0777);
			//write to the config.
			$fh = fopen('config.php', 'w') or die("Can't open config file");
			fwrite($fh, $config);
			
			echo "<ul>";
			echo "<li>Installing Database...</li>";
			
			//Install the databases, inserting the relevant installer options as parameters.
			$this->model->insert_tables(
							$this->input->post('hostname'),
							$this->input->post('dbusername'),
							$this->input->post('dbpassword'),
							$this->input->post('database'),
							$this->input->post('dbprefix')
			);
			
			echo "<li>Installing Admin User...</li>";
			
			$this->model->add_admin(
							$this->input->post('hostname'),
							$this->input->post('dbusername'),
							$this->input->post('dbpassword'),
							$this->input->post('database'),
							$this->input->post('dbprefix'),
							$this->input->post('username'),
							$this->input->post('password'),
							$this->input->post('email'),
							$this->input->post('display_name'),
							$encryption
			);
			
			echo "<li>Installing Settings...</li>";
			
			$this->model->add_settings(
							$this->input->post('hostname'),
							$this->input->post('dbusername'),
							$this->input->post('dbpassword'),
							$this->input->post('database'),
							$this->input->post('dbprefix'),
							$this->input->post('email'),
							$this->input->post('sitename'),
							$this->input->post('siteurl'),
							$this->input->post('display_name'),
							$this->input->post('system_email'),
							$this->input->post('contact_email')
			);
			echo "</ul>";
?>
			<p>The syntaxCMS installation is now successful. <a href="#" onClick="window.location.reload( true );">Click here to visit your new Website.</a></p>
<?php
		}
	}
}
